/*
 * Copyright 2009 Johnson Consulting Services
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * 
 */
package com.johnsoncs.gwt.application.client.validation.impl;

import java.util.ArrayList;
import java.util.List;

import com.johnsoncs.gwt.application.client.model.Data;
import com.johnsoncs.gwt.application.client.model.Model;
import com.johnsoncs.gwt.application.client.model.QueryPath;
import com.johnsoncs.gwt.application.client.model.Model.QueryCallback;
import com.johnsoncs.gwt.application.client.validation.ValidationContext;
import com.johnsoncs.gwt.application.client.validation.ValidationValue;

/**
 * @author wilj
 *
 */
public class ModelContext implements ValidationContext {
	private final Model model;

	public ModelContext(final Model model) {
		this.model = model;
	}

	/* (non-Javadoc)
	 * @see com.johnsoncs.gwt.application.client.validation.ValidationContext#getValue(com.johnsoncs.gwt.application.client.model.QueryPath)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<ValidationValue> getValues(final QueryPath queryPath) {
		final List<ValidationValue> results = new ArrayList<ValidationValue>();
		model.query(queryPath, new QueryCallback() {

			@Override
			public void onResult(final QueryPath path, final Object result) {
				if (result instanceof Data) {
					results.add(new DataValue((Data) result, path));
				} else {
					results.add(new LeafValue(result, path));
				}

			}
		});
		return results;
	}

}
